package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.SchedulWeekBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.ISchedulWeekService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 班次设置
 * @author 张鑫
 * 2017-10-10
 *
 */
@Controller
@RequestMapping("/schedulWeek")
public class SchedulWeekController {
	@Resource
	private ISchedulWeekService schedulWeekService;
	
	/**
	 * 验证是否排班时间有重叠
	 * @param request
	 * @param schedulWeekBean
	 * @return
	 */
	@RequestMapping(value = "/getVerifySchedulWeek.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getVerifySchedulWeek(HttpServletRequest request,@RequestBody SchedulWeekBean schedulWeekBean){
		if (schedulWeekService.getVerifySchedulWeek(schedulWeekBean)) {
			throw new ResultFaultException("5_00003");
		}
		return ResultMap.success();
	}
	
	/**
	 * 新增班次周表信息
	 * @param request
	 * @param schedulWeekBean
	 * @return
	 */
	@RequestMapping(value = "/saveSchedulWeek.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object saveSchedulWeek(HttpServletRequest request,@RequestBody SchedulWeekBean schedulWeekBean){
//		if (schedulWeekService.getSaveSchedulWeekValidate(schedulWeekBean)) {
			if(schedulWeekBean!=null){
				if (StringUtils.isBlank(schedulWeekBean.getSwName())
					||schedulWeekBean.getSwType()==null
					||schedulWeekBean.getFromDate()==null
					||schedulWeekBean.getThruDate()==null){
					throw new ResultFaultException("0_00001");
				}
				if (schedulWeekBean.getSwType()==1) {//周班次
					if (schedulWeekBean.getSwMonday()==null&&schedulWeekBean.getSwThursday()==null&&schedulWeekBean.getSwWednesday()==null
							&&schedulWeekBean.getSwThursday()==null&&schedulWeekBean.getSwFriday()==null&&schedulWeekBean.getSwSaturday()==null
							&&schedulWeekBean.getSwSunday()==null) {
						throw new ResultFaultException("0_00001");
					}
				}
				if (schedulWeekService.saveSchedulWeek(schedulWeekBean)) {
					return ResultMap.success();
				}
			}
//		}
		throw new ResultFaultException("0_00007");
	}
	/**
	 * 删除班次周表,同时删除相关的排班
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteSchedulWeek.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object deleteSchedulWeek(HttpServletRequest request){
		String swId = request.getParameter("swId");
		if (StringUtils.isBlank(swId)) {
			throw new ResultFaultException("0_00001");
		}
		if (schedulWeekService.getDeleteSchedulWeekValidate(swId)) {
			if(schedulWeekService.deleteSchedulWeek(swId)){
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("0_00009");
	}
	/**
	 * 修改班次周表
	 * @param request
	 * @param schedulWeekBean
	 * @return
	 */
	@RequestMapping(value = "/updateSchedulWeek.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object updateSchedulWeek(HttpServletRequest request,@RequestBody SchedulWeekBean schedulWeekBean){
		if (schedulWeekBean!=null) {
			if (schedulWeekBean.getSwId()==null
				||StringUtils.isBlank(schedulWeekBean.getSwName())
				||schedulWeekBean.getSwType()==null
				||schedulWeekBean.getFromDate()==null
				||schedulWeekBean.getThruDate()==null) {
				throw new ResultFaultException("0_00001");
			}
//			if (schedulWeekService.getUpdateSchedulWeekValidate(schedulWeekBean)) {//验证是否存在
				if (schedulWeekService.updateSchedulWeek(schedulWeekBean)) {
					return ResultMap.success();
				}
//			}
		}
		throw new ResultFaultException("0_00008");
	}
	/**
	 * 查询班次周表信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSchedulWeekList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getSchedulWeekList(HttpServletRequest request){
		return ResultMap.success(schedulWeekService.getSchedulWeekList());
	}
	
	/**
	 * 查询班次by班次ID
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSchedulWeekBySwId.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getSchedulWeekBySwId(HttpServletRequest request){
		String swId = request.getParameter("swId");
		if (StringUtils.isBlank(swId)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(schedulWeekService.getSchedulWeekBySwId(swId));
	}
	/**
	 * 导入特殊排班
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveSchedulWeekExcel.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object saveSchedulWeekExcel(HttpServletRequest request){
		String attachmentId = request.getParameter("attachmentId");
		if (StringUtils.isBlank(attachmentId)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(schedulWeekService.saveSchedulWeekExcel(Integer.parseInt(attachmentId)));
	}
	/**
	 * 模板下载
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/downloadAttendanceURL.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object downloadAttendanceURL(HttpServletRequest request) {
		return AppConstant.TEMPLATE_RESOURCE + "/特殊排班模板.xlsx";
	}
}
